-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memoryview is a context manager #2442
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! One remark though.
stdlib/3/builtins.pyi
Outdated
@@ -495,6 +495,8 @@ class memoryview(Sized, Container[int]): | |||
ndim = ... # type: int | |||
|
|||
def __init__(self, obj: Union[bytes, bytearray, memoryview]) -> None: ... | |||
def __enter__(self) -> memoryview: ... | |||
def __exit__(self, exc_type, exc_value, exc_tb) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please annotate __exit__
with:
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]) -> bool: ...
d5add1a
to
5d82e02
Compare
Thanks - and done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nearly. :)
stdlib/3/builtins.pyi
Outdated
@@ -495,6 +495,8 @@ class memoryview(Sized, Container[int]): | |||
ndim = ... # type: int | |||
|
|||
def __init__(self, obj: Union[bytes, bytearray, memoryview]) -> None: ... | |||
def __enter__(self) -> memoryview: ... | |||
def __exit__(self, exc_type: Optional[BaseException], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]) -> bool: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exc_type
should be Optional[Type[BaseException]]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, somehow I managed to drop the Type
while copy-pasting and then manually deleted the extra brackets. Fixed now!
Since Python 3.2, __enter__ returns self and __exit__ calls self.release()
5d82e02
to
0361dc1
Compare
Thank you! I will merge when the CI finishes successfully. |
Hurray! Thanks for the fast and helpful reviews 😍 |
Since Python 3.2, __enter__ returns self and __exit__ calls self.release()
Since Python 3.2,
memoryview
has been usable as a context manager, so this PR adds the appropriate methods to let Mypy know. Found while working on python-trio/trio#543.